import {
  buyerCallbackContract,
  sellerCallbackContract,
  buyerCancelContract,
  sellerCancelContract,
  getBuyerSignContract,
  getSellerSignContract,
  fileDownload
} from "@/api/contract";
import { downloadFile } from "@/util/common";
//查看合同
export function contractDetailMethod(identity, contractId) {
  let routeName;
  identity == 1
    ? (routeName = "buyerContractDetail")
    : (routeName = "sellerContractDetail");
  return this.$router.push({
    name: routeName,
    query: { contractId: contractId }
  });
}
//下载合同
export function downloadContractMethod(id) {
  fileDownload({
    contractId: id
  })
    .then(res => {
      downloadFile(res);
    })
    .catch(() => {});
}
//上传双签合同
export function contractUploadDoubleMethod(params, identity) {
  let routeName;
  identity == 1
    ? (routeName = "buyerUploadDoubleSign")
    : (routeName = "sellerUploadDoubleSign");
  let obj = {
    contractId: params.id,
    contractVersion: params.version,
    prePage: "contractListPage"
  };
  this.$router.push({
    name: routeName,
    query: params ? obj : ""
  });
}
//合同打回
export function contractBackToMethod(contractId, version, identity) {
  let requestFun;
  identity == 1
    ? (requestFun = buyerCallbackContract)
    : (requestFun = sellerCallbackContract);
  let that = this;
  return new Promise(resolve => {
    that.$confirm({
      title: "温馨提示",
      content: "是否确认要打回合同？",
      okText: "确认",
      cancelText: "取消",
      onOk() {
        requestFun({
          contractId: contractId,
          version: version
        }).then(() => {
          resolve("success");
          that.$message.success("合同已打回");
        });
      },
      onCancel() {
        // console.log("Cancel");
      }
    });
  });
}
//撤销合同
export function contractRelocateMethod(
  contractId,
  version,
  identity,
  toContractDetail
) {
  let requestFun;
  identity == 1
    ? (requestFun = buyerCancelContract)
    : (requestFun = sellerCancelContract);
  let that = this;
  return new Promise(resolve => {
    that.$confirm({
      title: "温馨提示",
      content: "是否确认要撤销合同？",
      okText: "确认",
      cancelText: "取消",
      onOk() {
        requestFun({
          contractId: contractId,
          version: version
        }).then(() => {
          resolve("success");
          that.$message.success("合同已撤销");
          if (toContractDetail) {
            that.contractDetailMethod(identity, contractId);
          }
        });
      },
      onCancel() {
        // console.log("Cancel");
      }
    });
  });
}
//合同签署
export function contractSigningMethod(identity, contractId, version) {
  let requestFun;
  identity == 1
    ? (requestFun = getBuyerSignContract)
    : (requestFun = getSellerSignContract);
  return new Promise((resolve, reject) => {
    requestFun({
      contractId: contractId,
      version: version,
      redirectUrl: "http://192.168.0.231:30000/home",
      terminalFlag: 4
    })
      .then(res => {
        resolve(res);
      })
      .catch(err => {
        reject(err);
      });
  });
}
